//
//  UIButton+customBtn.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/17.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "UIButton+customBtn.h"

@implementation UIButton (customBtn)
+ (UIButton *)titleOnLeftOfContentWithTitle:(NSString *)title andTitleColorString:(NSString *)colorStr andFont:(CGFloat)size andImageStr:(NSString *)imgStr andContainerView:(UIView *)supView andLocation:(ViewType)type andTitleOnLeft:(BOOL)isLeft andBackColor:(NSString *)backColorStr{
    UIButton *customView = [UIButton new];
    [customView setTitle:title forState:0];
    if (![backColorStr isEqualToString:@""]) {
        customView.backgroundColor = [UIColor custom_color:backColorStr];
    }
    [customView setTitleColor:[UIColor custom_color:colorStr] forState:0];
    customView.titleLabel.font = [UIFont customFontOfSize:size];
    customView.contentHorizontalAlignment = 0;
    [customView setImage:[UIImage imageNamed:imgStr] forState:0];
    [customView addTarget:[helpPublic getCurrentViewController:supView] action:@selector(bottomViewAction:) forControlEvents:UIControlEventTouchUpInside];
    [supView addSubview:customView];
    switch (type) {
        case right:
            [self rightLayoutSelf:customView andSupView:supView];
            break;
        case bottom:
            [self bottomLayoutSelf:customView andSupView:supView];
            break;
        default:
            break;
    }
    //设置btn title和image 位置
    if (isLeft) {
        [customView layoutIfNeeded];
        CGSize titleSize = customView.titleLabel.bounds.size;
        CGSize imageSize = customView.imageView.bounds.size;
        CGFloat interval = 1.0;
        customView.imageEdgeInsets = UIEdgeInsetsMake(0,titleSize.width + interval, 0, -(titleSize.width + interval));
        customView.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageSize.width + interval), 0, imageSize.width + interval);
    }
    
    return customView;
}

+ (void)rightLayoutSelf:(UIButton *)customView andSupView:(UIView *)supView {//右边
    [customView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(supView.mas_top).offset(10);
        make.right.equalTo(supView.mas_right);
        make.bottom.equalTo(supView.mas_bottom);
        make.width.equalTo(@50);
    }];
}
+ (void)bottomLayoutSelf:(UIButton *)customView andSupView:(UIView *)supView {//下边
    [customView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(iPhoneX ? @-34 : @0);
        make.width.mas_equalTo(kScreenWidth);
        make.height.equalTo(@50);
    }];
}
@end
